package cs2340.flyingkiwi.todoapp;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * This class simulates making a new account.
 * 
 * @author Group Flying Kiwi
 * @version 15 Mar 2012
 * 
 * It has a textfield for username, password1, password2, name, email, and confirm and
 * cancel buttons.
 * Pressing back button also cancels this activity.
 */

public class EditAccount extends Activity{
	private EditText mUsernameText;
	private EditText mPassword1Text;
	private EditText mPassword2Text;
	private EditText mNameText;
	private EditText mEmailText;
	
    private Long mRowId;
    private ToDoAppDbAdapter mDbHelper;
    
    /**
	 * onCreate method
	 * Displays the signup view and creates an Account id. Saves
	 * the data in the text fields if Create Account button is clicked.
	 * Does not save data if Cancel is clicked.
	 * @param savedInstanceState
	 */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	Log.d("EditAccount onCreate", "Begin");
        super.onCreate(savedInstanceState);
        mDbHelper = new ToDoAppDbAdapter(this);
        mDbHelper.open();
        
        setContentView(R.layout.signup);

        mUsernameText = (EditText) findViewById(R.id.username);
        mPassword1Text = (EditText) findViewById(R.id.password1);
        mPassword2Text = (EditText) findViewById(R.id.password2);
        mNameText = (EditText) findViewById(R.id.name);
        mEmailText = (EditText) findViewById(R.id.email);
        
        
        Button confirmButton = (Button) findViewById(R.id.create_account);
        Button cancelButton = (Button) findViewById(R.id.cancel_account);
        
        //Gets account row id from serialized savedInstanceState.
        mRowId = (savedInstanceState == null) ? null : 
        	(Long) savedInstanceState.getSerializable(ToDoAppDbAdapter.KEY_ACCOUNT_ROWID);
        
        //Get a new row id from the intent's extras.
        if (mRowId == null) {
        	Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(ToDoAppDbAdapter.KEY_ACCOUNT_ROWID) : null;
        }
        Log.d("EditAccount onCreate", "end");
      
        //Set up buttons.
        confirmButton.setOnClickListener(new View.OnClickListener() {
        	
            public void onClick(View view) {
            	Log.d("EditAccount confirmButton", "Begin");
            	saveState(); //Save the result when the button is clicked
                Log.d("EditAccount confirmButton", "After saveState");
               	setResult(RESULT_OK);
               	finish();
            }

        });
        
        cancelButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
               	setResult(RESULT_CANCELED);
               	finish();
            }

        });
    }
    
    
    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
    
    /**
     * This method saves the user input.
     */
    private void saveState(){
    	String username = mUsernameText.getText().toString();
    	String password = mPassword1Text.getText().toString();
    	String name = mNameText.getText().toString();
    	String email = mEmailText.getText().toString();
		
    	Toast.makeText(EditAccount.this, "WELCOME!",
				Toast.LENGTH_LONG).show();    	
    	
    	long id = mDbHelper.createAccount(username, password, name, email);
    	if(id>0)
    		mRowId = id;
    }
}

